home *** CD-ROM | disk | FTP | other *** search
/ LG Super CD / LG Super CD.iso / bitpim / bitpim-0.62-setup.exe / {app} / bitpim.exe / warnings.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2003-11-06  |  8.0 KB  |  270 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.3)
  3.  
  4. import sys
  5. import types
  6. import linecache
  7. __all__ = [
  8.     'warn',
  9.     'showwarning',
  10.     'formatwarning',
  11.     'filterwarnings',
  12.     'resetwarnings']
  13. filters = []
  14. defaultaction = 'default'
  15. onceregistry = { }
  16.  
  17. def warn(message, category = None, stacklevel = 1):
  18.     if isinstance(message, Warning):
  19.         category = message.__class__
  20.     
  21.     if category is None:
  22.         category = UserWarning
  23.     
  24.     
  25.     try:
  26.         caller = sys._getframe(stacklevel)
  27.     except ValueError:
  28.         globals = sys.__dict__
  29.         lineno = 1
  30.  
  31.     globals = caller.f_globals
  32.     lineno = caller.f_lineno
  33.     if '__name__' in globals:
  34.         module = globals['__name__']
  35.     else:
  36.         module = '<string>'
  37.     filename = globals.get('__file__')
  38.     if filename:
  39.         fnl = filename.lower()
  40.         if fnl.endswith('.pyc') or fnl.endswith('.pyo'):
  41.             filename = filename[:-1]
  42.         
  43.     elif module == '__main__':
  44.         filename = sys.argv[0]
  45.     
  46.     if not filename:
  47.         filename = module
  48.     
  49.     registry = globals.setdefault('__warningregistry__', { })
  50.     warn_explicit(message, category, filename, lineno, module, registry)
  51.  
  52.  
  53. def warn_explicit(message, category, filename, lineno, module = None, registry = None):
  54.     if module is None:
  55.         module = filename
  56.         if module[-3:].lower() == '.py':
  57.             module = module[:-3]
  58.         
  59.     
  60.     if registry is None:
  61.         registry = { }
  62.     
  63.     if isinstance(message, Warning):
  64.         text = str(message)
  65.         category = message.__class__
  66.     else:
  67.         text = message
  68.         message = category(message)
  69.     key = (text, category, lineno)
  70.     if registry.get(key):
  71.         return None
  72.     
  73.     for item in filters:
  74.         (action, msg, cat, mod, ln) = item
  75.         if (msg is None or msg.match(text)) and issubclass(category, cat):
  76.             if (msg is None or mod.match(module)) and ln == 0 or lineno == ln:
  77.                 break
  78.                 continue
  79.     else:
  80.         action = defaultaction
  81.     if action == 'ignore':
  82.         registry[key] = 1
  83.         return None
  84.     
  85.     if action == 'error':
  86.         raise message
  87.     
  88.     if action == 'once':
  89.         registry[key] = 1
  90.         oncekey = (text, category)
  91.         if onceregistry.get(oncekey):
  92.             return None
  93.         
  94.         onceregistry[oncekey] = 1
  95.     elif action == 'always':
  96.         pass
  97.     elif action == 'module':
  98.         registry[key] = 1
  99.         altkey = (text, category, 0)
  100.         if registry.get(altkey):
  101.             return None
  102.         
  103.         registry[altkey] = 1
  104.     elif action == 'default':
  105.         registry[key] = 1
  106.     else:
  107.         raise RuntimeError('Unrecognized action (%s) in warnings.filters:\n %s' % (`action`, str(item)))
  108.     showwarning(message, category, filename, lineno)
  109.  
  110.  
  111. def showwarning(message, category, filename, lineno, file = None):
  112.     if file is None:
  113.         file = sys.stderr
  114.     
  115.     
  116.     try:
  117.         file.write(formatwarning(message, category, filename, lineno))
  118.     except IOError:
  119.         pass
  120.  
  121.  
  122.  
  123. def formatwarning(message, category, filename, lineno):
  124.     s = '%s:%s: %s: %s\n' % (filename, lineno, category.__name__, message)
  125.     line = linecache.getline(filename, lineno).strip()
  126.     if line:
  127.         s = s + '  ' + line + '\n'
  128.     
  129.     return s
  130.  
  131.  
  132. def filterwarnings(action, message = '', category = Warning, module = '', lineno = 0, append = 0):
  133.     import re
  134.     item = (action, re.compile(message, re.I), category, re.compile(module), lineno)
  135.     if append:
  136.         filters.append(item)
  137.     else:
  138.         filters.insert(0, item)
  139.  
  140.  
  141. def simplefilter(action, category = Warning, lineno = 0, append = 0):
  142.     item = (action, None, category, None, lineno)
  143.     if append:
  144.         filters.append(item)
  145.     else:
  146.         filters.insert(0, item)
  147.  
  148.  
  149. def resetwarnings():
  150.     filters[:] = []
  151.  
  152.  
  153. class _OptionError(Exception):
  154.     pass
  155.  
  156.  
  157. def _processoptions(args):
  158.     for arg in args:
  159.         
  160.         try:
  161.             _setoption(arg)
  162.         continue
  163.         except _OptionError:
  164.             msg = None
  165.             print >>sys.stderr, 'Invalid -W option ignored:', msg
  166.             continue
  167.         
  168.  
  169.     
  170.  
  171.  
  172. def _setoption(arg):
  173.     import re
  174.     parts = arg.split(':')
  175.     if len(parts) > 5:
  176.         raise _OptionError('too many fields (max 5): %s' % `arg`)
  177.     
  178.     while len(parts) < 5:
  179.         parts.append('')
  180.     (action, message, category, module, lineno) = [ s.strip() for s in parts ]
  181.     action = _getaction(action)
  182.     message = re.escape(message)
  183.     category = _getcategory(category)
  184.     module = re.escape(module)
  185.     if lineno:
  186.         
  187.         try:
  188.             lineno = int(lineno)
  189.             if lineno < 0:
  190.                 raise ValueError
  191.         except (ValueError, OverflowError):
  192.             None if module else []
  193.             None if module else []
  194.             raise _OptionError('invalid lineno %s' % `lineno`)
  195.         except:
  196.             None if module else []<EXCEPTION MATCH>(ValueError, OverflowError)
  197.         
  198.  
  199.     None if module else []
  200.     lineno = 0
  201.     filterwarnings(action, message, category, module, lineno)
  202.  
  203.  
  204. def _getaction(action):
  205.     if not action:
  206.         return 'default'
  207.     
  208.     if action == 'all':
  209.         return 'always'
  210.     
  211.     for a in [
  212.         'default',
  213.         'always',
  214.         'ignore',
  215.         'module',
  216.         'once',
  217.         'error']:
  218.         if a.startswith(action):
  219.             return a
  220.             continue
  221.     
  222.     raise _OptionError('invalid action: %s' % `action`)
  223.  
  224.  
  225. def _getcategory(category):
  226.     import re
  227.     if not category:
  228.         return Warning
  229.     
  230.     if re.match('^[a-zA-Z0-9_]+$', category):
  231.         
  232.         try:
  233.             cat = eval(category)
  234.         except NameError:
  235.             raise _OptionError('unknown warning category: %s' % `category`)
  236.         except:
  237.             None<EXCEPTION MATCH>NameError
  238.         
  239.  
  240.     None<EXCEPTION MATCH>NameError
  241.     i = category.rfind('.')
  242.     module = category[:i]
  243.     klass = category[i + 1:]
  244.     
  245.     try:
  246.         m = __import__(module, None, None, [
  247.             klass])
  248.     except ImportError:
  249.         raise _OptionError('invalid module name: %s' % `module`)
  250.  
  251.     
  252.     try:
  253.         cat = getattr(m, klass)
  254.     except AttributeError:
  255.         raise _OptionError('unknown warning category: %s' % `category`)
  256.  
  257.     if not isinstance(cat, types.ClassType) or not issubclass(cat, Warning):
  258.         raise _OptionError('invalid warning category: %s' % `category`)
  259.     
  260.     return cat
  261.  
  262. if __name__ == '__main__':
  263.     import __main__
  264.     sys.modules['warnings'] = __main__
  265.     _test()
  266. else:
  267.     _processoptions(sys.warnoptions)
  268.     simplefilter('ignore', category = OverflowWarning, append = 1)
  269.     simplefilter('ignore', category = PendingDeprecationWarning, append = 1)
  270.